package com.ip;

import mapgenerator.*;
import com.graphics.ImageIdentity;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 * Created on Feb 21, 2009, 00:24:53 AM
 *
 * @author Sajarwo Anggai
 */
public class IPPanelWidget extends JPanel {

    private Main comp;
    private DefaultMutableTreeNode top;
    private JScrollPane treeView;
    private JTree tree;

    public IPPanelWidget(Main comp) {
        this.comp = comp;
        setLayout(null);
        reloadAll();
    }

    public void initComponents() {
        top = new DefaultMutableTreeNode("List of Pattern");
        tree = new JTree(top);
        treeView = new JScrollPane(tree);
        Dimension minimumSize = new Dimension(170, 400);
        treeView.setMinimumSize(minimumSize);
        treeView.setPreferredSize(minimumSize);
        tree.addTreeSelectionListener(comp.ipControlPanel);
    }

    /**
     * Menampilkan semua widget disebelah kiri pada Image Manager
     */
    public void reloadAll() {
        initComponents();
        DefaultMutableTreeNode leaf = null;
        ArrayList al = comp.ipContainer.getListImagePattern();
        leaf = addNode("Groups Pattern");
        for (int i = 0; i < al.size(); i++) {
            ImageIdentity ii = (ImageIdentity) al.get(i);
            if (!ii.isSprite()) {
                leaf.add(addNode(ii));
            }
        }
        top.add(leaf);

        leaf = addNode("Groups Sprite");
        for (int i = 0; i < al.size(); i++) {
            ImageIdentity ii = (ImageIdentity) al.get(i);
            if (ii.isSprite()) {
                leaf.add(addNode(ii));
            }
        }
        top.add(leaf);
        tree.expandRow(0);
        treeView.repaint();
        treeView.revalidate();
        comp.ImagePatternLeftPanelRender.removeAll();
        comp.ImagePatternLeftPanelRender.add(treeView);
        comp.ImagePatternLeftPanelRender.repaint();
        comp.ImagePatternLeftPanelRender.revalidate();
        comp.ImagePatternRightPanelRender.removeAll();
        comp.ImagePatternRightPanelRender.repaint();
        comp.ImagePatternRightPanelRender.revalidate();
        comp.ipControlPanel.showImgeManagerInfo(false);
    }
    public DefaultMutableTreeNode addNode(Object obj) {
        return new DefaultMutableTreeNode(obj);
    }

    public JTree getTree() {
        return tree;
    }
    
}
